/*
   @Copyright:LibreOJ
   @Author:   tjyemail
   @Problem:  https://loj.ac/problem/119
   @Language: C++
   @Datetime: 20-08-13 15:07
   */
// Bellman Ford, time O(VE)

#include <bits/stdc++.h>
using namespace std;

#define VERTEX 2505
#define EDGE 6205
struct Edge {
	int a, b, c;
	Edge(int a=0, int b=0, int c=0):a(a), b(b), c(c){}
} edges[EDGE*2];
int dists[VERTEX];
//int path[VERTEX];

void bellmanford(int v, int e, int s, int t) {
	int dist = 0;
	for(dists[s]=0; v--; ) { // must have v-1 edges
		dist = dists[t];
		// update dist[s->k.b] from edge k : dist[s->k.a->k.b]
		for(int k=0; k<e*2; ++k) {
			if (dists[edges[k].a]==INT_MAX) continue;
			if (dists[edges[k].b]>dists[edges[k].a]+edges[k].c) {
				dists[edges[k].b] = dists[edges[k].a]+edges[k].c;
				//path[edges[k].b] = edges[k].a; // record path
			}
		}
		dist = dist == dists[t] ? dist : INT_MIN; // has negtive circle
	}
	printf("%d\n", dist);
}

int main()
{
	int v, e, s, t, a, b, c; // vertex edge, start -> target
	scanf("%d %d %d %d", &v, &e, &s, &t);
	for(int i=0; i++<v; dists[i]=INT_MAX);
	for(int i=0; i<e; ++i) {
		scanf("%d %d %d", &a, &b, &c); // a->b, cost
		edges[(i<<1)+0] = Edge(a, b, c);
		edges[(i<<1)+1] = Edge(b, a, c); // undirect graph
	}
	bellmanford(v, e, s, t);
	// shortest path s -> ... -> t
	//for(printf("%d",a=t); a!=s; printf("<-%d",a=path[a]));
	//printf("\n");
	return 0;
}
